package com.lyk.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.common.dao.BaseDao;
import com.common.entity.A_customer;
import com.common.entity.A_transfer;
import com.common.entity.PageBean;
import com.cxh.dao.A_goodsDao;
import com.cxh.dao.impl.A_goodsDaoImpl;
import com.lyk.dao.A_customerDao;
import com.lyk.dao.A_transferDao;

public class A_transferDaoImpl extends BaseDao<A_transfer> implements A_transferDao {

	@Override
	public List<A_transfer> findByCondition(Map<String, Object> mp) {
		List<Object> paramList=new ArrayList<Object>();
		StringBuffer sf=new StringBuffer();
		sf.append(" select * from a_transfer where 1=1 ");
		if(mp!=null && mp.size()>0){
				if(mp.get("transfer_id")!=null){
					sf.append("and transfer_id = ? ");
					paramList.add(mp.get("transfer_id"));
				}
				if(mp.get("transfer_date")!=null){
					sf.append("and to_char(transfer_date,'yyyy-MM-dd')=? ");
					paramList.add(mp.get("transfer_date"));
				}
		}
		return findBySql(sf.toString(), paramList.toArray());
	}

	@Override
	public int delA_customer(Integer n) {
		String sql = "delete a_transfer where transfer_id = ?";
		return this.executeUpdate(sql, new Object[]{n});
	}
	public A_transfer getEntity(ResultSet rs){
		try {
			A_transfer transfer = new A_transfer();
			transfer.setTransfer_id(rs.getInt("transfer_id"));
			transfer.setTransfer_outid(rs.getInt("transfer_outid"));
			transfer.setAmount_out(rs.getInt("amount_out"));
			transfer.setPoundage(rs.getInt("poundage"));
			transfer.setTransfer_inid(rs.getInt("transfer_inid"));
			transfer.setAmount_in(rs.getInt("amount_in"));
			transfer.setGoods_id(rs.getInt("goods_id"));
			transfer.setTransfer_date(rs.getDate("transfer_date"));
			return transfer;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public PageBean<A_transfer> findPageByCondition(int pageIndex,
			int pageSize, Map<String, Object> mp) {
		PageBean<A_transfer> pageBean=new PageBean<A_transfer>();
		
		//1���ȳ�ʼ�����õ���ֵ
		pageBean.setPageIndex(pageIndex);
		pageBean.setPageSize(pageSize);
		
		//2��ƴ��sql���
		List<Object> paramList=new ArrayList<Object>();
		StringBuffer querySql=new StringBuffer();
		querySql.append(" select * from a_transfer where 1=1");
		if(mp!=null && mp.size()>0){
			if(mp.get("transfer_id")!=null){
				querySql.append("and transfer_id = ? ");
				paramList.add(mp.get("transfer_id"));
			}
			if(mp.get("transfer_date")!=null){
				querySql.append("and to_char(transfer_date,'yyyy-MM-dd')=? ");
				paramList.add(mp.get("transfer_date"));
			}
		}
		
		//3����ȡ�ܼ�¼��
		pageBean.setTotalCount(this.getCount(querySql.toString(), paramList.toArray()));
		//4����ѯ��ҳ���
		if(pageBean.getTotalCount()>0){
			List<A_transfer> list = findPage(pageBean, paramList, querySql);
			pageBean.setList(list);
		}
		
		return pageBean;
	}

	private List<A_transfer> findPage(PageBean<A_transfer> pageBean,
			List<Object> paramList, StringBuffer querySql) {
		List<A_transfer> list=new ArrayList<A_transfer>();
		StringBuffer queryPageSql=new StringBuffer();
		queryPageSql.append(" select * from (");
		queryPageSql.append("  select tmp.*,rownum rn from ( ");
		queryPageSql.append(querySql.toString());
		queryPageSql.append("  ) tmp ");
		queryPageSql.append(" ) where rn>? and rn<=? ");
		int startRow=(pageBean.getPageIndex()-1)*pageBean.getPageSize();
		int endRow=pageBean.getPageIndex()*pageBean.getPageSize();
		paramList.add(startRow);
		paramList.add(endRow);
		Connection conn=this.getConnection();
		PreparedStatement prs=null;
		ResultSet rs=null;
		try {
			conn=this.getConnection();
			//3.2 �����伯
			prs=conn.prepareStatement(queryPageSql.toString());
			//3.2.1�� ����ռλ�����ֵ
			Object[] objs=paramList.toArray();
			if(objs!=null && objs.length>0){
				for(int i=0;i<objs.length;i++){
					prs.setObject(i+1, objs[i]);
				}
			}
			//3.3��ִ�в�ѯ
			rs=prs.executeQuery();
			
			while(rs.next()){
				A_transfer transfer = new A_transfer();
				A_customerDao customerDao = new A_customerDaoImpl();
				A_goodsDao goodsDao = new A_goodsDaoImpl();
				List<A_customer> customerList = new ArrayList<A_customer>();
				transfer.setTransfer_id(rs.getInt("transfer_id"));
				transfer.setTransfer_outid(rs.getInt("transfer_outid"));
				transfer.setAmount_out(rs.getInt("amount_out"));
				transfer.setPoundage(rs.getInt("poundage"));
				transfer.setTransfer_inid(rs.getInt("transfer_inid"));
				transfer.setAmount_in(rs.getInt("amount_in"));
				transfer.setGoods_id(rs.getInt("goods_id"));
				transfer.setTransfer_date(rs.getDate("transfer_date"));
				transfer.setGoods(goodsDao.findById(rs.getInt("goods_id")));
				customerList.add(customerDao.findById(rs.getInt("transfer_outid")));
				customerList.add(customerDao.findById(rs.getInt("transfer_inid")));
				transfer.setCustomerList(customerList);
				list.add(transfer);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	
	@Override
	public int sumPoundage() {
		String attrName = "poundage";
		String sql = "a_transfer";
		return getSum(attrName, sql, null);
	}
}
